home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / STATS.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  1KB  |  42 lines

  1. .I 10 1
  2. #include <stdlib.h>
  3. .I 15 37
  4.       int i, ch, hist = 0;
  5.       long n = 0L;
  6.       double mean = 0., stdev = 0., ftmp;
  7.       static unsigned bins[256];
  8.       FILE *infile;
  9.  
  10.       assert(infile = fopen(argv[1], "rb"));
  11.       while (!feof(infile))
  12.       {
  13.             if (EOF == (ch = fgetc(infile)))
  14.                   break;
  15.             bins[ch] += 1;
  16.             ++n;
  17.       }
  18.       fclose(infile);
  19.       for (i = 0; i < 256; ++i)
  20.       {
  21.             mean += (double)(bins[i]);
  22.             if (bins[i])
  23.                   ++hist;
  24.       }
  25.       mean /= 256.;
  26.       for (i = 0; i < 256; ++i)
  27.       {
  28.             ftmp = (double)(bins[i]) - mean;
  29.             stdev += (ftmp * ftmp);
  30.       }
  31.       ftmp  = stdev / 255.;
  32.       stdev = sqrt(ftmp);
  33.       printf("%ld Characters were read from %s\n"
  34.             "There are an average of %f occurances of each character\n"
  35.             "%d Characters out of 256 possible were used\n"
  36.             "The standard deviation is %f\n"
  37.             "The coefficient of variation is %f%%\n",
  38.             n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  39.       return EXIT_SUCCESS;
  40. }
  41. .D 16 36
  42.